Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polymorphize cont #6

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Polymorphize cont #6

wants to merge 19 commits into from

Conversation

lcnr
Copy link
Owner

@lcnr lcnr commented Oct 22, 2021

No description provided.

@lcnr lcnr force-pushed the polymorphize-cont branch 4 times, most recently from c65cdaa to dc4e348 Compare October 22, 2021 18:24
@lcnr lcnr force-pushed the polymorphize-cont branch 4 times, most recently from 2d2b95f to 528a781 Compare November 20, 2021 17:33
lcnr pushed a commit that referenced this pull request Dec 7, 2023
Change prefetch to avoid deadlock

Was abled to reproduce the deadlock in rust-lang#118205 and created a coredump when it happen. When looking at the backtraces  I noticed that the prefetch of exported_symbols (Thread 17 frame 4) started after the "actual" exported_symbols (Thread 2 frame 18) but it also is working on some of the collect_crate_mono_items (Thread 17 frame12 ) that Thread 2 is blocked on resulting in a deadlock.

This PR results in less parallell work that can be done at the same time but from what I can find we do not call the query exported_symbols from multiple places in the same join call any more.

```
Thread 17 (Thread 0x7f87b6299700 (LWP 11370)):
#0  syscall () at ../sysdeps/unix/sysv/linux/x86_64/syscall.S:38
#1  0x00007f87be5166a9 in <parking_lot::condvar::Condvar>::wait_until_internal () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#2  0x00007f87be12d854 in <rustc_query_system::query::job::QueryLatch>::wait_on () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#3  0x00007f87bd27d16f in rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::VecCache<rustc_span::def_id::CrateNum, rustc_middle::query::erase::Erased<[u8; 16]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#4  0x00007f87bd0b5b6a in rustc_query_impl::query_impl::exported_symbols::get_query_non_incr::__rust_end_short_backtrace () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#5  0x00007f87bdaebb0a in rustc_metadata::rmeta::encoder::encode_metadata::{closure#1}::{closure#1} () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#6  0x00007f87bdae1509 in rayon_core::join::join_context::call_b::<core::option::Option<rustc_data_structures::marker::FromDyn<&[(rustc_middle::middle::exported_symbols::ExportedSymbol, rustc_middle::middle::exported_symbols::SymbolExportInfo)]>>, rayon_core::join::join::call<core::option::Option<rustc_data_structures::marker::FromDyn<&[(rustc_middle::middle::exported_symbols::ExportedSymbol, rustc_middle::middle::exported_symbols::SymbolExportInfo)]>>, rustc_data_structures::sync::parallel::enabled::join<rustc_metadata::rmeta::encoder::encode_metadata::{closure#1}::{closure#0}, rustc_metadata::rmeta::encoder::encode_metadata::{closure#1}::{closure#1}, (), &[(rustc_middle::middle::exported_symbols::ExportedSymbol, rustc_middle::middle::exported_symbols::SymbolExportInfo)]>::{closure#0}::{closure#1}>::{closure#0}>::{closure#0} () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#7  0x00007f87bdae32ff in <rayon_core::job::StackJob<rayon_core::latch::SpinLatch, rayon_core::join::join_context::call_b<core::option::Option<rustc_data_structures::marker::FromDyn<&[(rustc_middle::middle::exported_symbols::ExportedSymbol, rustc_middle::middle::exported_symbols::SymbolExportInfo)]>>, rayon_core::join::join::call<core::option::Option<rustc_data_structures::marker::FromDyn<&[(rustc_middle::middle::exported_symbols::ExportedSymbol, rustc_middle::middle::exported_symbols::SymbolExportInfo)]>>, rustc_data_structures::sync::parallel::enabled::join<rustc_metadata::rmeta::encoder::encode_metadata::{closure#1}::{closure#0}, rustc_metadata::rmeta::encoder::encode_metadata::{closure#1}::{closure#1}, (), &[(rustc_middle::middle::exported_symbols::ExportedSymbol, rustc_middle::middle::exported_symbols::SymbolExportInfo)]>::{closure#0}::{closure#1}>::{closure#0}>::{closure#0}, core::option::Option<rustc_data_structures::marker::FromDyn<&[(rustc_middle::middle::exported_symbols::ExportedSymbol, rustc_middle::middle::exported_symbols::SymbolExportInfo)]>>> as rayon_core::job::Job>::execute () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#8  0x00007f87b8338823 in <rayon_core::registry::WorkerThread>::wait_until_cold () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#9  0x00007f87bc2edbaf in rayon_core::join::join_context::<rayon::iter::plumbing::bridge_producer_consumer::helper<rayon::vec::DrainProducer<rustc_middle::mir::mono::MonoItem>, rayon::iter::for_each::ForEachConsumer<rustc_data_structures::sync::parallel::enabled::par_for_each_in<rustc_middle::mir::mono::MonoItem, alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>>::{closure#0}, rayon::iter::plumbing::bridge_producer_consumer::helper<rayon::vec::DrainProducer<rustc_middle::mir::mono::MonoItem>, rayon::iter::for_each::ForEachConsumer<rustc_data_structures::sync::parallel::enabled::par_for_each_in<rustc_middle::mir::mono::MonoItem, alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>>::{closure#1}, (), ()>::{closure#0} () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#10 0x00007f87bc2ed313 in rayon_core::registry::in_worker::<rayon_core::join::join_context<rayon::iter::plumbing::bridge_producer_consumer::helper<rayon::vec::DrainProducer<rustc_middle::mir::mono::MonoItem>, rayon::iter::for_each::ForEachConsumer<rustc_data_structures::sync::parallel::enabled::par_for_each_in<rustc_middle::mir::mono::MonoItem, alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>>::{closure#0}, rayon::iter::plumbing::bridge_producer_consumer::helper<rayon::vec::DrainProducer<rustc_middle::mir::mono::MonoItem>, rayon::iter::for_each::ForEachConsumer<rustc_data_structures::sync::parallel::enabled::par_for_each_in<rustc_middle::mir::mono::MonoItem, alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>>::{closure#1}, (), ()>::{closure#0}, ((), ())> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#11 0x00007f87bc2db2a4 in rayon::iter::plumbing::bridge_producer_consumer::helper::<rayon::vec::DrainProducer<rustc_middle::mir::mono::MonoItem>, rayon::iter::for_each::ForEachConsumer<rustc_data_structures::sync::parallel::enabled::par_for_each_in<rustc_middle::mir::mono::MonoItem, alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#12 0x00007f87bc2eead2 in <rayon_core::job::StackJob<rayon_core::latch::SpinLatch, rayon_core::join::join_context::call_b<(), rayon::iter::plumbing::bridge_producer_consumer::helper<rayon::vec::DrainProducer<rustc_middle::mir::mono::MonoItem>, rayon::iter::for_each::ForEachConsumer<rustc_data_structures::sync::parallel::enabled::par_for_each_in<rustc_middle::mir::mono::MonoItem, alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>>::{closure#1}>::{closure#0}, ()> as rayon_core::job::Job>::execute () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#13 0x00007f87b8338823 in <rayon_core::registry::WorkerThread>::wait_until_cold () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#14 0x00007f87be52d1f9 in <rayon_core::registry::ThreadBuilder>::run () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#15 0x00007f87b8461c57 in <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#3}::{closure#0}::{closure#0}::{closure#0}, ()> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#16 0x00007f87b846e465 in rustc_span::set_session_globals_then::<(), rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#3}::{closure#0}::{closure#0}::{closure#0}> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#17 0x00007f87b844f282 in <<crossbeam_utils::thread::ScopedThreadBuilder>::spawn<<rayon_core::ThreadPoolBuilder>::build_scoped<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#3}::{closure#0}::{closure#0}, rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#3}::{closure#0}::{closure#1}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}, ()>::{closure#0} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#18 0x00007f87b846af58 in <<std::thread::Builder>::spawn_unchecked_<alloc::boxed::Box<dyn core::ops::function::FnOnce<(), Output = ()> + core::marker::Send>, ()>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#19 0x00007f87b7898e85 in std::sys::unix::thread::Thread::new::thread_start () from /home/andjo403/.rustup/toolchains/stage1/lib/libstd-d570b0650d35d951.so
rust-lang#20 0x00007f87b7615609 in start_thread (arg=<optimized out>) at pthread_create.c:477
rust-lang#21 0x00007f87b7755133 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 2 (Thread 0x7f87b729b700 (LWP 11368)):
#0  syscall () at ../sysdeps/unix/sysv/linux/x86_64/syscall.S:38
#1  0x00007f87b7887b51 in std::sys::unix::locks::futex_condvar::Condvar::wait () from /home/andjo403/.rustup/toolchains/stage1/lib/libstd-d570b0650d35d951.so
#2  0x00007f87b8339478 in <rayon_core::sleep::Sleep>::sleep () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#3  0x00007f87b83387c3 in <rayon_core::registry::WorkerThread>::wait_until_cold () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#4  0x00007f87bc2edbaf in rayon_core::join::join_context::<rayon::iter::plumbing::bridge_producer_consumer::helper<rayon::vec::DrainProducer<rustc_middle::mir::mono::MonoItem>, rayon::iter::for_each::ForEachConsumer<rustc_data_structures::sync::parallel::enabled::par_for_each_in<rustc_middle::mir::mono::MonoItem, alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>>::{closure#0}, rayon::iter::plumbing::bridge_producer_consumer::helper<rayon::vec::DrainProducer<rustc_middle::mir::mono::MonoItem>, rayon::iter::for_each::ForEachConsumer<rustc_data_structures::sync::parallel::enabled::par_for_each_in<rustc_middle::mir::mono::MonoItem, alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>>::{closure#1}, (), ()>::{closure#0} () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#5  0x00007f87bc2ed313 in rayon_core::registry::in_worker::<rayon_core::join::join_context<rayon::iter::plumbing::bridge_producer_consumer::helper<rayon::vec::DrainProducer<rustc_middle::mir::mono::MonoItem>, rayon::iter::for_each::ForEachConsumer<rustc_data_structures::sync::parallel::enabled::par_for_each_in<rustc_middle::mir::mono::MonoItem, alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>>::{closure#0}, rayon::iter::plumbing::bridge_producer_consumer::helper<rayon::vec::DrainProducer<rustc_middle::mir::mono::MonoItem>, rayon::iter::for_each::ForEachConsumer<rustc_data_structures::sync::parallel::enabled::par_for_each_in<rustc_middle::mir::mono::MonoItem, alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>>::{closure#1}, (), ()>::{closure#0}, ((), ())> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#6  0x00007f87bc2db50c in <rayon::vec::IntoIter<rustc_middle::mir::mono::MonoItem> as rayon::iter::ParallelIterator>::for_each::<rustc_data_structures::sync::parallel::enabled::par_for_each_in<rustc_middle::mir::mono::MonoItem, alloc::vec::Vec<rustc_middle::mir::mono::MonoItem>, rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#7  0x00007f87bc2e8cd7 in <rustc_session::session::Session>::time::<(), rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#8  0x00007f87bc2b8f2c in rustc_monomorphize::collector::collect_crate_mono_items () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
#9  0x00007f87bc2c30d9 in rustc_monomorphize::partitioning::collect_and_partition_mono_items () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#10 0x00007f87bcf2cde6 in rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 24]>> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#11 0x00007f87bd156a3c in <rustc_query_impl::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, ())>>::call_once () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#12 0x00007f87bd1c6a7d in rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 24]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#13 0x00007f87bd15df40 in rustc_query_impl::query_impl::collect_and_partition_mono_items::get_query_non_incr::__rust_end_short_backtrace () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#14 0x00007f87bd7a0ad9 in rustc_codegen_ssa::back::symbol_export::exported_symbols_provider_local () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#15 0x00007f87bcf29acb in rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::exported_symbols::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 16]>> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#16 0x00007f87bcfdb350 in <rustc_query_impl::query_impl::exported_symbols::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::CrateNum)>>::call_once () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#17 0x00007f87bd27d64f in rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::VecCache<rustc_span::def_id::CrateNum, rustc_middle::query::erase::Erased<[u8; 16]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#18 0x00007f87bd0b5b6a in rustc_query_impl::query_impl::exported_symbols::get_query_non_incr::__rust_end_short_backtrace () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#19 0x00007f87bda927ce in rustc_middle::query::plumbing::query_get_at::<rustc_query_system::query::caches::VecCache<rustc_span::def_id::CrateNum, rustc_middle::query::erase::Erased<[u8; 16]>>> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#20 0x00007f87bda9c93f in <rustc_metadata::rmeta::encoder::EncodeContext>::encode_crate_root () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#21 0x00007f87bdaa6ef7 in rustc_metadata::rmeta::encoder::encode_metadata_impl () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#22 0x00007f87bdae0b77 in rayon_core::join::join_context::<rayon_core::join::join::call<core::option::Option<rustc_data_structures::marker::FromDyn<()>>, rustc_data_structures::sync::parallel::enabled::join<rustc_metadata::rmeta::encoder::encode_metadata::{closure#0}, rustc_metadata::rmeta::encoder::encode_metadata::{closure#1}, (), ()>::{closure#0}::{closure#0}>::{closure#0}, rayon_core::join::join::call<core::option::Option<rustc_data_structures::marker::FromDyn<()>>, rustc_data_structures::sync::parallel::enabled::join<rustc_metadata::rmeta::encoder::encode_metadata::{closure#0}, rustc_metadata::rmeta::encoder::encode_metadata::{closure#1}, (), ()>::{closure#0}::{closure#1}>::{closure#0}, core::option::Option<rustc_data_structures::marker::FromDyn<()>>, core::option::Option<rustc_data_structures::marker::FromDyn<()>>>::{closure#0} () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#23 0x00007f87bdaded2f in rayon_core::registry::in_worker::<rayon_core::join::join_context<rayon_core::join::join::call<core::option::Option<rustc_data_structures::marker::FromDyn<()>>, rustc_data_structures::sync::parallel::enabled::join<rustc_metadata::rmeta::encoder::encode_metadata::{closure#0}, rustc_metadata::rmeta::encoder::encode_metadata::{closure#1}, (), ()>::{closure#0}::{closure#0}>::{closure#0}, rayon_core::join::join::call<core::option::Option<rustc_data_structures::marker::FromDyn<()>>, rustc_data_structures::sync::parallel::enabled::join<rustc_metadata::rmeta::encoder::encode_metadata::{closure#0}, rustc_metadata::rmeta::encoder::encode_metadata::{closure#1}, (), ()>::{closure#0}::{closure#1}>::{closure#0}, core::option::Option<rustc_data_structures::marker::FromDyn<()>>, core::option::Option<rustc_data_structures::marker::FromDyn<()>>>::{closure#0}, (core::option::Option<rustc_data_structures::marker::FromDyn<()>>, core::option::Option<rustc_data_structures::marker::FromDyn<()>>)> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#24 0x00007f87bdaa5a03 in rustc_metadata::rmeta::encoder::encode_metadata () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#25 0x00007f87bdaed628 in rustc_metadata::fs::encode_and_write_metadata () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#26 0x00007f87b86608be in rustc_interface::passes::start_codegen () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#27 0x00007f87b8664946 in <rustc_middle::ty::context::GlobalCtxt>::enter::<<rustc_interface::queries::Queries>::codegen_and_build_linker::{closure#0}, core::result::Result<rustc_interface::queries::Linker, rustc_span::ErrorGuaranteed>> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#28 0x00007f87b864db00 in <rustc_interface::queries::Queries>::codegen_and_build_linker () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#29 0x00007f87b849400f in <rustc_interface::interface::Compiler>::enter::<rustc_driver_impl::run_compiler::{closure#0}::{closure#0}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_span::ErrorGuaranteed>> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#30 0x00007f87b846e067 in rustc_span::set_source_map::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}::{closure#0}> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#31 0x00007f87b844dc13 in <rayon_core::thread_pool::ThreadPool>::install::<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0} () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#32 0x00007f87b84509a1 in <rayon_core::job::StackJob<rayon_core::latch::LatchRef<rayon_core::latch::LockLatch>, <rayon_core::registry::Registry>::in_worker_cold<<rayon_core::thread_pool::ThreadPool>::install<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>> as rayon_core::job::Job>::execute () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#33 0x00007f87b8338823 in <rayon_core::registry::WorkerThread>::wait_until_cold () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#34 0x00007f87be52d1f9 in <rayon_core::registry::ThreadBuilder>::run () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#35 0x00007f87b8461c57 in <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#3}::{closure#0}::{closure#0}::{closure#0}, ()> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#36 0x00007f87b846e465 in rustc_span::set_session_globals_then::<(), rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#3}::{closure#0}::{closure#0}::{closure#0}> () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#37 0x00007f87b844f282 in <<crossbeam_utils::thread::ScopedThreadBuilder>::spawn<<rayon_core::ThreadPoolBuilder>::build_scoped<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#3}::{closure#0}::{closure#0}, rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#3}::{closure#0}::{closure#1}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}, ()>::{closure#0} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#38 0x00007f87b846af58 in <<std::thread::Builder>::spawn_unchecked_<alloc::boxed::Box<dyn core::ops::function::FnOnce<(), Output = ()> + core::marker::Send>, ()>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} () from /home/andjo403/.rustup/toolchains/stage1/lib/librustc_driver-70ddb84e8f7ce707.so
rust-lang#39 0x00007f87b7898e85 in std::sys::unix::thread::Thread::new::thread_start () from /home/andjo403/.rustup/toolchains/stage1/lib/libstd-d570b0650d35d951.so
rust-lang#40 0x00007f87b7615609 in start_thread (arg=<optimized out>) at pthread_create.c:477
rust-lang#41 0x00007f87b7755133 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

```

fixes rust-lang#118205
fixes rust-lang#117759 from the latest logs it is the same query map as in rust-lang#118205
fixes rust-lang#118529
fixes rust-lang#117784
cc rust-lang#118206

r? `@SparrowLii`
lcnr pushed a commit that referenced this pull request Jul 15, 2024
lcnr pushed a commit that referenced this pull request Oct 17, 2024
…raheemdev

Optimize `Box::default` and `Arc::default` to construct more types in place

Both the `Arc` and `Box` `Default` impls currently call `T::default()` before allocating, and then moving the resulting `T` into the allocation.

Most `Default` impls are trivial, which should in theory allow
LLVM to construct `T: Default` directly in the `Box` allocation when calling
`<Box<T>>::default()`.

However, the allocation may fail, which necessitates calling `T`'s destructor if it has one.
If the destructor is non-trivial, then LLVM has a hard time proving that it's
sound to elide, which makes it construct `T` on the stack first, and then copy it into the allocation.

Change both of these impls to allocate first, and then call `T::default` into the uninitialized allocation, so that LLVM doesn't have to prove that it's sound to elide the destructor/initial stack copy.

For example, given the following Rust code:

```rust
#[derive(Default, Clone)]
struct Foo {
    x: Vec<u8>,
    z: String,
    y: Vec<u8>,
}

#[no_mangle]
pub fn src() -> Box<Foo> {
    Box::default()
}
```

<details open>
<summary>Before this PR:</summary>

```llvm
`@__rust_no_alloc_shim_is_unstable` = external global i8

; drop_in_place() generated in case the allocation fails

; core::ptr::drop_in_place<playground::Foo>
; Function Attrs: nounwind nonlazybind uwtable
define internal fastcc void `@"_ZN4core3ptr36drop_in_place$LT$playground..Foo$GT$17hff376aece491233bE"(ptr` noalias nocapture noundef readonly align 8 dereferenceable(72) %_1) unnamed_addr #0 personality ptr `@rust_eh_personality` {
start:
  %_1.val = load i64, ptr %_1, align 8
  %0 = icmp eq i64 %_1.val, 0
  br i1 %0, label %bb6, label %"_ZN63_$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Allocator$GT$10deallocate17heaa87468709346b1E.exit.i.i.i4.i"

"_ZN63_$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Allocator$GT$10deallocate17heaa87468709346b1E.exit.i.i.i4.i": ; preds = %start
  %1 = getelementptr inbounds i8, ptr %_1, i64 8
  %_1.val6 = load ptr, ptr %1, align 8, !nonnull !3, !noundef !3
  tail call void `@__rust_dealloc(ptr` noundef nonnull %_1.val6, i64 noundef %_1.val, i64 noundef 1) #8
  br label %bb6

bb6:                                              ; preds = %"_ZN63_$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Allocator$GT$10deallocate17heaa87468709346b1E.exit.i.i.i4.i", %start
  %2 = getelementptr inbounds i8, ptr %_1, i64 24
  %.val9 = load i64, ptr %2, align 8
  %3 = icmp eq i64 %.val9, 0
  br i1 %3, label %bb5, label %"_ZN63_$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Allocator$GT$10deallocate17heaa87468709346b1E.exit.i.i.i4.i.i11"

"_ZN63_$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Allocator$GT$10deallocate17heaa87468709346b1E.exit.i.i.i4.i.i11": ; preds = %bb6
  %4 = getelementptr inbounds i8, ptr %_1, i64 32
  %.val10 = load ptr, ptr %4, align 8, !nonnull !3, !noundef !3
  tail call void `@__rust_dealloc(ptr` noundef nonnull %.val10, i64 noundef %.val9, i64 noundef 1) #8
  br label %bb5

bb5:                                              ; preds = %"_ZN63_$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Allocator$GT$10deallocate17heaa87468709346b1E.exit.i.i.i4.i.i11", %bb6
  %5 = getelementptr inbounds i8, ptr %_1, i64 48
  %.val4 = load i64, ptr %5, align 8
  %6 = icmp eq i64 %.val4, 0
  br i1 %6, label %"_ZN4core3ptr46drop_in_place$LT$alloc..vec..Vec$LT$u8$GT$$GT$17hb5ca95423e113cf7E.exit16", label %"_ZN63_$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Allocator$GT$10deallocate17heaa87468709346b1E.exit.i.i.i4.i15"

"_ZN63_$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Allocator$GT$10deallocate17heaa87468709346b1E.exit.i.i.i4.i15": ; preds = %bb5
  %7 = getelementptr inbounds i8, ptr %_1, i64 56
  %.val5 = load ptr, ptr %7, align 8, !nonnull !3, !noundef !3
  tail call void `@__rust_dealloc(ptr` noundef nonnull %.val5, i64 noundef %.val4, i64 noundef 1) #8
  br label %"_ZN4core3ptr46drop_in_place$LT$alloc..vec..Vec$LT$u8$GT$$GT$17hb5ca95423e113cf7E.exit16"

"_ZN4core3ptr46drop_in_place$LT$alloc..vec..Vec$LT$u8$GT$$GT$17hb5ca95423e113cf7E.exit16": ; preds = %bb5, %"_ZN63_$LT$alloc..alloc..Global$u20$as$u20$core..alloc..Allocator$GT$10deallocate17heaa87468709346b1E.exit.i.i.i4.i15"
  ret void
}

; Function Attrs: nonlazybind uwtable
define noalias noundef nonnull align 8 ptr `@src()` unnamed_addr #1 personality ptr `@rust_eh_personality` {
start:

; alloca to place `Foo` in.
  %_1 = alloca [72 x i8], align 8
  call void `@llvm.lifetime.start.p0(i64` 72, ptr nonnull %_1)
  store i64 0, ptr %_1, align 8
  %_2.sroa.4.0._1.sroa_idx = getelementptr inbounds i8, ptr %_1, i64 8
  store ptr inttoptr (i64 1 to ptr), ptr %_2.sroa.4.0._1.sroa_idx, align 8
  %_2.sroa.5.0._1.sroa_idx = getelementptr inbounds i8, ptr %_1, i64 16
  %_3.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %_1, i64 32
  call void `@llvm.memset.p0.i64(ptr` noundef nonnull align 8 dereferenceable(16) %_2.sroa.5.0._1.sroa_idx, i8 0, i64 16, i1 false)
  store ptr inttoptr (i64 1 to ptr), ptr %_3.sroa.4.0..sroa_idx, align 8
  %_3.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %_1, i64 40
  %_4.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %_1, i64 56
  call void `@llvm.memset.p0.i64(ptr` noundef nonnull align 8 dereferenceable(16) %_3.sroa.5.0..sroa_idx, i8 0, i64 16, i1 false)
  store ptr inttoptr (i64 1 to ptr), ptr %_4.sroa.4.0..sroa_idx, align 8
  %_4.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %_1, i64 64
  store i64 0, ptr %_4.sroa.5.0..sroa_idx, align 8
  %0 = load volatile i8, ptr `@__rust_no_alloc_shim_is_unstable,` align 1, !noalias !4
  %_0.i.i.i = tail call noalias noundef align 8 dereferenceable_or_null(72) ptr `@__rust_alloc(i64` noundef 72, i64 noundef 8) #8, !noalias !4
  %1 = icmp eq ptr %_0.i.i.i, null
  br i1 %1, label %bb2.i, label %"_ZN5alloc5boxed12Box$LT$T$GT$3new17h0864de14f863a27aE.exit"

bb2.i:                                            ; preds = %start
; invoke alloc::alloc::handle_alloc_error
  invoke void `@_ZN5alloc5alloc18handle_alloc_error17h98142d0d8d74161bE(i64` noundef 8, i64 noundef 72) #9
          to label %.noexc unwind label %cleanup.i

.noexc:                                           ; preds = %bb2.i
  unreachable

cleanup.i:                                        ; preds = %bb2.i
  %2 = landingpad { ptr, i32 }
          cleanup
; call core::ptr::drop_in_place<playground::Foo>
  call fastcc void `@"_ZN4core3ptr36drop_in_place$LT$playground..Foo$GT$17hff376aece491233bE"(ptr` noalias noundef nonnull align 8 dereferenceable(72) %_1) rust-lang#10
  resume { ptr, i32 } %2

"_ZN5alloc5boxed12Box$LT$T$GT$3new17h0864de14f863a27aE.exit": ; preds = %start

; Copy from stack to heap if allocation is successful
  call void `@llvm.memcpy.p0.p0.i64(ptr` noundef nonnull align 8 dereferenceable(72) %_0.i.i.i, ptr noundef nonnull align 8 dereferenceable(72) %_1, i64 72, i1 false)
  call void `@llvm.lifetime.end.p0(i64` 72, ptr nonnull %_1)
  ret ptr %_0.i.i.i
}

```
</details>

<details>
<summary>After this PR</summary>

```llvm
; Notice how there's no `drop_in_place()` generated as well

define noalias noundef nonnull align 8 ptr `@src()` unnamed_addr #0 personality ptr `@rust_eh_personality` {
start:
; no stack allocation

  %0 = load volatile i8, ptr `@__rust_no_alloc_shim_is_unstable,` align 1
  %_0.i.i.i.i.i = tail call noalias noundef align 8 dereferenceable_or_null(72) ptr `@__rust_alloc(i64` noundef 72, i64 noundef 8) #5
  %1 = icmp eq ptr %_0.i.i.i.i.i, null
  br i1 %1, label %bb3.i, label %"_ZN5alloc5boxed16Box$LT$T$C$A$GT$13new_uninit_in17h80d6355ef4b73ea3E.exit"

bb3.i:                                            ; preds = %start
; call alloc::alloc::handle_alloc_error
  tail call void `@_ZN5alloc5alloc18handle_alloc_error17h98142d0d8d74161bE(i64` noundef 8, i64 noundef 72) #6
  unreachable

"_ZN5alloc5boxed16Box$LT$T$C$A$GT$13new_uninit_in17h80d6355ef4b73ea3E.exit": ; preds = %start
; construct `Foo` directly into the allocation if successful

  store i64 0, ptr %_0.i.i.i.i.i, align 8
  %_8.sroa.4.0._1.sroa_idx = getelementptr inbounds i8, ptr %_0.i.i.i.i.i, i64 8
  store ptr inttoptr (i64 1 to ptr), ptr %_8.sroa.4.0._1.sroa_idx, align 8
  %_8.sroa.5.0._1.sroa_idx = getelementptr inbounds i8, ptr %_0.i.i.i.i.i, i64 16
  %_8.sroa.7.0._1.sroa_idx = getelementptr inbounds i8, ptr %_0.i.i.i.i.i, i64 32
  tail call void `@llvm.memset.p0.i64(ptr` noundef nonnull align 8 dereferenceable(16) %_8.sroa.5.0._1.sroa_idx, i8 0, i64 16, i1 false)
  store ptr inttoptr (i64 1 to ptr), ptr %_8.sroa.7.0._1.sroa_idx, align 8
  %_8.sroa.8.0._1.sroa_idx = getelementptr inbounds i8, ptr %_0.i.i.i.i.i, i64 40
  %_8.sroa.10.0._1.sroa_idx = getelementptr inbounds i8, ptr %_0.i.i.i.i.i, i64 56
  tail call void `@llvm.memset.p0.i64(ptr` noundef nonnull align 8 dereferenceable(16) %_8.sroa.8.0._1.sroa_idx, i8 0, i64 16, i1 false)
  store ptr inttoptr (i64 1 to ptr), ptr %_8.sroa.10.0._1.sroa_idx, align 8
  %_8.sroa.11.0._1.sroa_idx = getelementptr inbounds i8, ptr %_0.i.i.i.i.i, i64 64
  store i64 0, ptr %_8.sroa.11.0._1.sroa_idx, align 8
  ret ptr %_0.i.i.i.i.i
}
```

</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant